package net.csdn.business.webhook.controller;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.annotation.Inner;
import net.csdn.business.common.domain.request.webhook.WebhookRedeliveryDTO;
import net.csdn.business.common.domain.vo.WebHookEvent;
import net.csdn.business.webhook.config.WebHookEventConfig;
import net.csdn.business.webhook.service.INotifyService;
import net.csdn.business.webhook.service.IWebhookConfigEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WebHookEventController
 * @Description
 * @Author zhangyl
 * @Date 2023/4/26 11:02
 */
@Inner
@RestController
@RequestMapping("/api/v1/webhook/event")
@Slf4j
public class WebHookEventController {

    @Autowired
    private WebHookEventConfig webHookEventConfig;

    @Autowired
    private IWebhookConfigEventService webhookConfigEventService;


    /**
     * 查询组织、项目、issue的webhook的事件
     */
    @GetMapping("/{type}")
    public List<WebHookEvent> getEvent(@PathVariable("type")String type){
        List<WebHookEvent> result=new ArrayList<>();
        Map<String, List<WebHookEvent>> map=webHookEventConfig.getList();
        if(map!=null){
            result=map.get(type);
        }
        return result;
    }


    /**
     * webhookEvent重新发送通知
     */
    @PostMapping("/redelivery")
    public Integer redelivery(@RequestBody @Validated WebhookRedeliveryDTO params)throws Exception{
        log.info("webhook-redelivery-params:{}", JSON.toJSONString(params));
        webhookConfigEventService.redelivery(params);
        return 1;
    }

}